package com.guigu.base.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.base.entity.CoreOrg;
import com.guigu.base.entity.Fleet;
import com.guigu.base.entity.GlobalUser;
import com.guigu.base.entity.TruckDriver;
import com.guigu.base.entity.dto.GlobalUserDTO;
import com.guigu.base.service.CoreOrgService;
import com.guigu.base.service.FleetService;
import com.guigu.base.service.GlobalUserService;
import com.guigu.base.service.TruckDriverService;
import com.guigu.tms.result.R;
import com.guigu.tms.result.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 全局账号 前端控制器
 * </p>
 *
 * @author TaoLong
 * @since 2021-11-18
 */
@RestController
@RequestMapping("/base/global-user")
@Api(tags = "全局用户管理")
public class GlobalUserController {
    @Autowired
    private GlobalUserService globalUserService;

    @Autowired
    private TruckDriverService truckDriverService;

    @Autowired
    private FleetService fleetService;

    @Autowired
    private CoreOrgService coreOrgService;

    /**
     * 分页查询用户
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/{pageNum}/{pageSize}")
    @ApiOperation("分页查询用户")
    public R<IPage<GlobalUser>> queryAll(@PathVariable String pageNum,
                                         @PathVariable String pageSize) {
        IPage<GlobalUser> page = new Page<>();
        page.setCurrent(Integer.valueOf(pageNum));
        page.setSize(Integer.parseInt(pageSize));
        IPage<GlobalUser> userIPage = globalUserService.page(page, null);
        return new R<>("2000", userIPage);
    }

    /**
     * 司机端用户登录
     *
     * @param account
     * @param password
     * @return
     */
    @GetMapping("/login/{account}/{password}")
    @ApiOperation("司机端登录")
    public R<Map> driverLogin(@PathVariable("account") String account,
                                         @PathVariable("password") String password) {
        if(null == account){
            throw new IllegalArgumentException("参数异常!");
        }
        if(null == password){
            throw new IllegalArgumentException("参数异常!");
        }
        GlobalUser user = globalUserService.getOne(new QueryWrapper<GlobalUser>().
                eq("account", account).eq("password", password));
        Map<String, Object> map = new HashMap<>();
        if(user != null){
            //司机
            TruckDriver truckDriver = truckDriverService.
                    getOne(new QueryWrapper<TruckDriver>().
                            eq("user_id", user.getId()));
            map.put("truckDriver",truckDriver);
            //车队
            Fleet fleet = fleetService.getById(truckDriver.getFleetId());
            map.put("fleet",fleet);
            //所属组织
            CoreOrg coreOrg = coreOrgService.getById(fleet.getAgencyId());
            map.put("coreOrg",coreOrg);
            map.put("user",user);
            return new R<Map>("2000", "登录成功",true,map);
        }else{
            map.put("user",user);
            return new R<Map>("2000", "登录失败",false);
        }

    }

    /**
     * 司机提货网点信息
     *
     * @param startAgencyId
     * @param endAgencyId
     * @return
     */
    @GetMapping("/driverInfo/{startAgencyId}/{endAgencyId}")
    @ApiOperation("司机提货信息")
    public R<Map> driverInfo(@PathVariable("startAgencyId") String startAgencyId,
                              @PathVariable("endAgencyId") String endAgencyId) {
        if(null == startAgencyId){
            throw new IllegalArgumentException("参数异常!");
        }
        if(null == endAgencyId){
            throw new IllegalArgumentException("参数异常!");
        }
        Map<String, Object> map=new HashMap<>();
        CoreOrg startAgency = coreOrgService.getById(startAgencyId);
        CoreOrg endAgency = coreOrgService.getById(endAgencyId);

        map.put("startAgency",startAgency);
        map.put("endAgency",endAgency);
        return new R<Map>("2000", map);
    }


    /**
     * 添加全局用户
     *
     * @param user
     * @return
     */
    @PostMapping("/")
    @ApiOperation("添加全局用户")
    public R addGlobalUser(@RequestBody GlobalUser user) {
        user.setReadonly(true);
        user.setTenantCode(String.valueOf(System.currentTimeMillis()));

        user.setCreateTime(new Date());
        boolean b = globalUserService.save(user);
        return new R("2000", "用户添加成功!", true);
    }


    /**
     * 查询全部用户
     *
     * @return
     */
    @GetMapping("/")
    @ApiOperation("查询全部用户")
    public R<List<GlobalUser>> queryAll() {
        return new R<>("2000", globalUserService.list(null));
    }

    /**
     * 根据快递员Id查询其员工信息
     *
     * @param courierId
     * @return
     */
    @ApiOperation("根据快递员Id查询其员工信息")
    @GetMapping("/queryAccordingToTheCourierId/{courierId}")
    public GlobalUser queryAccordingToTheCourierId(@PathVariable("courierId") String courierId) {
        GlobalUser according = globalUserService.queryAccordingToTheCourierId(courierId);
        return according;
    }

    /**
     * 根据id查询全局用户信息
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询全局用户信息")
    @GetMapping("/findGlobalUserById/{id}")
    public R<GlobalUser> findGlobalUserById(@PathVariable String id) {
        GlobalUser user = globalUserService.getById(id);
        return new R<>(ResultCode.SUCCESS_CODE, user);
    }
}

